app.controller('BusPriceListCtrl', function ($scope, ajax, $modal, toaster) {

    var inserter = new Req.POST({ resource: $scope.resource });
    var deleter = new Req.DELETE({ resource: $scope.resource });
    var updater = new Req.PUT({ resource: $scope.resource });

    
    
    $scope.typeList = [
         	{ id: 0, name: "无折扣" },
        	{ id: 1, name: "等额递减" },
        	{ id: 2, name: "等差递减" }
    ];
    
    $scope.reload = $scope.page.loader('price/bs/{pageIndex}/{pageSize}/page');
    $scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
    $scope.reload(1);
    $scope.queryMap = {};
    $scope.query = function () { $scope.reload(1, $scope.queryMap); };

    $scope.query = function() {
        if ($scope.typeList.selected != undefined) {
            $scope.queryMap.type = $scope.typeList.selected.id;
        } else if ($scope.typeList.selected == undefined) {
            delete $scope.queryMap.type;
        }
        $scope.reload(1, $scope.queryMap);
    };
    // 新增、更新
    $scope.write = function (size, current_data, submit_type) {
        $scope.current_data = angular.copy(current_data);
        var modalInstance = $modal.open({
            templateUrl: 'template/bus/price/writeModal.html?v=74d294a5e3',
            controller: 'BusPriceModalCtrl',
            size: size,
            resolve: {
                param: function () {
                    return {
                        "data": $scope.current_data,
                        "type": submit_type
                    };
                }
            }
        });

        modalInstance.result.then(function (param) {
            if (param.type == 'update') {
                updater.fields = param.data;
                updater.id = param.data.id;
                updater.resource = 'price/bs/update';
                $scope.page.mod(updater, $scope.reload);
            } else if (param.type == 'insert') {
                inserter.fields = param.data;
                inserter.resource = 'price/bs/insert';
                $scope.page.add(inserter, $scope.reload);
            }
        });
    };


    //删除
    $scope.del = function (size, current_data, submit_type) {
        var modalInstance = $scope.delModal(size, current_data, submit_type);
        modalInstance.result.then(function (param) {
            if (param.type == 'delete') {
                deleter.id = param.data.id;
                deleter.resource = 'price/bs/' + deleter.id + '/delete';
                $scope.page.del(deleter, $scope.reload);
            }
        });
    };

});
